package com.yzt.plugin_widget.voicerecordview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.yzt.plugin_widget.R;

public class RecordControllerView extends View {

    private final static int MAX_RADIUS = 70;
    private final static int MAX_INT = 140;

    private int mWidth;
    private Path mPath;
    private Paint mPaint;
    private int mRecordBtnLeft;
    private int mRecordBtnRight;
    private int mRecordBtnTop;
    private int mRecordBtnBottom;
    private RecordVoiceButton mRecordVoiceBtn;

    private int mCurrentState = 0;
    private float mNowX;
    private final static int INIT_STATE = 0;
    private final static int MOVING_LEFT = 1;
    private final static int MOVE_ON_LEFT = 2;
    private final static int MOVING_RIGHT = 3;
    private final static int MOVE_ON_TOP = 4;

    private Bitmap mCancelBmp;
    private Bitmap mCancelPresBmp;
    private Rect mRightRect;
    private OnRecordActionListener mListener;
    private boolean isShowDeleteButton = true;

    private boolean isPlaying;

    public RecordControllerView(Context context) {
        super(context);
        init();
    }

    public RecordControllerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mPath = new Path();
        mPaint = new Paint();
        mCancelBmp = BitmapFactory.decodeResource(getResources(), R.drawable.aurora_recordvoice_cancel_record);
        mCancelPresBmp = BitmapFactory.decodeResource(getResources(), R.drawable.aurora_recordvoice_cancel_record_pres);
    }

    public void setWidth(int width) {
        mWidth = width;
        mRightRect = new Rect(mWidth / 2 - MAX_RADIUS, MAX_INT + MAX_RADIUS, mWidth / 2 + MAX_RADIUS, MAX_INT + MAX_RADIUS * 3);
    }

    public void showDeleteButton(boolean isShow) {
        isShowDeleteButton = isShow;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        switch (mCurrentState) {
            case INIT_STATE:
            case MOVING_LEFT:
            case MOVING_RIGHT:
                if (isPlaying && isShowDeleteButton) {
                    canvas.drawBitmap(mCancelBmp, null, mRightRect, mPaint);
                }
                break;
            case MOVE_ON_LEFT:
                break;
            case MOVE_ON_TOP:
                if (isShowDeleteButton) {
                    canvas.drawBitmap(mCancelPresBmp, null, mRightRect, mPaint);
                }
                break;
            default:
                break;
        }
    }

    public void onActionDown() {
        isPlaying = true;
        postInvalidate();
        if (mListener != null) {
            mListener.onStart();
        }
    }

    public void onActionMove(float x, float y) {
        mNowX = x;
        int cx = mWidth / 2;
        if ((x >= cx - MAX_RADIUS && x <= cx + MAX_RADIUS) &&
                (y <= -1 * (mRecordBtnTop - (MAX_INT + MAX_RADIUS * 3))
                        && y >= -1 * (mRecordBtnTop - (MAX_INT + MAX_RADIUS)))) {
            mCurrentState = MOVE_ON_TOP;
            if (mListener != null) {
                mListener.onMovedTop();
            }
        } else {
            mCurrentState = INIT_STATE;
            if (mListener != null) {
                mListener.onMoving();
            }
        }
        postInvalidate();
    }

    public void setRecordButton(RecordVoiceButton button) {
        mRecordBtnLeft = button.getLeft();
        mRecordBtnRight = button.getRight();
        mRecordBtnTop = button.getTop();
        mRecordBtnBottom = button.getBottom();
        mRecordVoiceBtn = button;
    }

    public void onActionUp() {
        isPlaying = false;
        switch (mCurrentState) {
            case MOVE_ON_LEFT:
                mRecordVoiceBtn.finishRecord(true);
                if (mListener != null) {
                    mListener.onLeftUpTapped();
                }
                break;
            case MOVE_ON_TOP:
                mRecordVoiceBtn.cancelRecord();
                if (mListener != null) {
                    mListener.onRightUpTapped();
                }
                break;
            default:
                mRecordVoiceBtn.finishRecord(false);
                if (mListener != null) {
                    mListener.onRightUpTapped();
                }
        }
        mCurrentState = INIT_STATE;
        postInvalidate();
    }

    public void resetState() {
        mCurrentState = INIT_STATE;
        postInvalidate();
    }

    public void setOnControllerListener(OnRecordActionListener listener) {
        mListener = listener;
    }

    public interface OnRecordActionListener {

        void onStart();

        void onMoving();

        void onMovedLeft();

        void onMovedTop();

        void onRightUpTapped();

        void onLeftUpTapped();
    }
}
